#include "PauseState.h"
#include "Game.h"
#include "GameState.h"
#include "MenuState.h"
#include "MenuButton.h"
#include "TextureManager.h"
#include "InputHandler.h"


const std::string PauseState::s_pauseID = "PAUSE";

void PauseState::s_pauseToMain(){
    TheGame::Instance()->getStateMachine()->changeState(new MenuState());
}

void PauseState::s_resumePlay(){
    TheGame::Instance()->getStateMachine()->popState();
}

void PauseState::update(){
    
    for (int i = 0; i < m_gameObjects.size(); i++){
        m_gameObjects[i]->update();
    }
}

void PauseState::render(){
    for(int i = 0; i <m_gameObjects.size(); i++){
        m_gameObjects[i]->draw();
    }
}

bool PauseState::onEnter(){
#if 0    
    if (!TheTextureManager::Instance()->load("assets/resume_button.png",
                "resumebutton",
                TheGame::Instance()->getRenderer()
                )){
        return false;
    }
    if (!TheTextureManager::Instance()->load("assets/main_menu_button.png",
                "mainbutton",
                TheGame::Instance()->getRenderer()
                )){
        return false;
    }

    GameObject * mainbutton = new MenuButton(
            new LoaderParams(
                    200, 100,
                    200, 80,
                    "mainbutton"
                ), s_pauseToMain
            );
    GameObject * resumebutton = new MenuButton(
            new LoaderParams(
                    200, 300,
                    200, 80,
                    "resumebutton"
                ), s_resumePlay
            );

    m_gameObjects.push_back(mainbutton);
    m_gameObjects.push_back(resumebutton);
#endif

    StateParser stateParser;
    stateParser.parseState(
            "assets/test.xml",
            s_pauseID,
            &m_gameObjects,
            &m_textureIDList
            );
    
    m_callbacks.push_back(0);
    m_callbacks.push_back(s_pauseToMain);
    m_callbacks.push_back(s_resumePlay);
    setCallbacks(m_callbacks);

    std::cout << " entering PauseState" << std::endl;
    return true;
}

bool PauseState::onExit(){
    for (int i = 0; i < m_gameObjects.size(); i++){
        m_gameObjects[i]->clean();
    }
    m_gameObjects.clear();
#if 0
    TheTextureManager::Instance()
        ->clearFromTextureMap("resumtbutton");
    TheTextureManager::Instance()
        ->clearFromTextureMap("mainbutton");
    TheInputHandler::Instance()->reset();
#endif
    for ( int i = 0; i < m_textureIDList.size(); i++){
        TheTextureManager::Instance()->
            clearFromTextureMap(m_textureIDList[i]);
    }

    std::cout << "exiting PauseState"<< std::endl;
    return true;
}

void PauseState::setCallbacks(const std::vector<Callback> & callbacks){

    for ( int i = 0; i < m_gameObjects.size(); i++)
    {
        if (dynamic_cast<MenuButton*>(m_gameObjects[i])){
            MenuButton * pButton =
            dynamic_cast<MenuButton*>(m_gameObjects[i]);
            pButton->setCallback(callbacks[pButtonn->getCallbackID()]);
        }

    }
}


